package com.tzbank.wisdom.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tzbank.utils.String2Utils;
import com.tzbank.wisdom.dao.SingleDeviceLoginMapper;
import com.tzbank.wisdom.entity.SingleDeviceLogin;
import com.tzbank.wisdom.service.SingleDeviceLoginService;

import net.sf.json.JSONObject;

@Service
public class SingleDeviceLoginServiceImpl implements SingleDeviceLoginService {

	@Resource
	private SingleDeviceLoginMapper singleDeviceLoginMapper;

	@Transactional
	@Override
	public boolean addSingleDeviceLogin(String recordId, Map<String, String> reqBody, Map<String, Object> loginResult) {

		JSONObject dataLogin = (JSONObject) loginResult.get("data");
		String userId = dataLogin.getString("clientSeqNo");
		String token = dataLogin.getString("token");

		String deviceId = reqBody.get("deviceId");
		String deviceType = reqBody.get("deviceType");
		String version = reqBody.get("version");
		String network = reqBody.get("network");
		String wifiName = reqBody.get("wifiName");
		String address = reqBody.get("address");
		
		//经纬度
		String longitude = reqBody.get("longitude");
		String latitude = reqBody.get("latitude");
		

		SingleDeviceLogin singleDeviceLogin = new SingleDeviceLogin();

		singleDeviceLogin.setRecordId("SDL" + String2Utils.getGenerateUUID());
		singleDeviceLogin.setUserId(userId);
		singleDeviceLogin.setToken(token);
		
		singleDeviceLogin.setLongitude(longitude);
		singleDeviceLogin.setLatitude(latitude);
		
		singleDeviceLogin.setDeviceId(deviceId);
		singleDeviceLogin.setDeviceType(deviceType);

		singleDeviceLogin.setNetwork(network);
		singleDeviceLogin.setWifiName(wifiName);
		singleDeviceLogin.setAddress(address);
		singleDeviceLogin.setVersion(version);
		singleDeviceLogin.setLoginState("1");
		
		singleDeviceLogin.setLastRecordId(recordId);

		singleDeviceLogin.setLoginDate(new Date());

		int res = singleDeviceLoginMapper.inserSelective(singleDeviceLogin);
		return res > 0;
	}

	@Override
	public SingleDeviceLogin findByUserId(String userId) {
		return singleDeviceLoginMapper.findByUserId(userId);
	}

	@Transactional
	@Override
	public boolean deleteSingleDeviceLogin(String recordId, String userId, String token) {
		return singleDeviceLoginMapper.deleteSingleDeviceLogin(recordId, userId, token);
	}

	@Transactional
	@Override
	public boolean updateBindState(String deviceId, String userId, String bindState) {
		return singleDeviceLoginMapper.updateBindState(deviceId,userId,bindState);
	}

	@Override
	public List<SingleDeviceLogin> findByUserIdList(String userId) {
		return singleDeviceLoginMapper.findByUserIdList(userId);
	}

	@Override
	public SingleDeviceLogin findByUserIdAndDeviceId(String userId, String deviceId) {
		return singleDeviceLoginMapper.findByUserIdAndDeviceId(userId, deviceId);
	}
}
